#include "decorations.h"

extern int G_W, G_H;

DECORATIONS::DECORATIONS(QRectF rect, DECORATIONS_TYPE type, QString mission, int count = 0, int factor = 0, bool is_cover = false, CASH *cash=0):QObject(), QGraphicsItemGroup()
{

    this->cash=cash;

    this->rect = QRectF(rect.x(), rect.y(), ((rect.width()*factor)), rect.height());
    scene_rect = rect;

    this->type = type;
    this->mission = mission;
    this->is_cover = is_cover;

    this->factor = factor;
    this->count = count*factor;
    factor_speed = 0;

    vect_pix = new QVector<QPixmap>();

    load_Origin();
    Generate();
    init();
}

QRectF DECORATIONS::boundingRect() const
{
    return this->rect;
}

void DECORATIONS::init()
{
//
    if(!is_cover)
        this->setPos(-(this->boundingRect().width()/2 - 500), 0);
    else
        this->setPos(-(this->boundingRect().width()/2 - 500), - this->boundingRect().height() - scene_rect.width());

//настройка скорости анимации и глубины в сцене
    switch (type) {
    case CLOWDS:
        this->setZValue(3+factor);
        basic_anim_speed = 7;
        basic_move_speed = 3;

        break;

    case SMOKE:
        this->setZValue(2);
        basic_anim_speed = 6;
        basic_move_speed = 3;

        break;

    default:
        break;
    }

}

void DECORATIONS::Generate_Clowds()
{
    this->childItems().clear();

    int rand_pix = 0;
    int rand_coords_x = 0;
    int rand_coords_y = 0;

    for(int i=0;i<count;i++){
        rand_pix = qrand()%(vect_pix->count());

        GRAPHICS_PIXMAP_ITEM *item = new GRAPHICS_PIXMAP_ITEM(vect_pix->at(rand_pix), this);

        int x_a, x_b, y_a, y_b;

        x_a = ((int)rect.x() - 300);
        x_b = (int)rect.width();

        y_a = (int)rect.y();
        y_b = ((int)rect.height() - (int)item->boundingRect().width());

        rand_coords_x = x_a + qrand()%(x_b - x_a + 1);
        rand_coords_y = y_a + qrand()%(y_b - y_a + 1);

        item->setPos(rand_coords_x, rand_coords_y);
        item->setZValue(0);
    }
}

void DECORATIONS::load_Origin()
{
    QString str_type;

    switch (type) {
        case CLOWDS:
            str_type = "/clowds/";
        break;

        case SMOKE:
            str_type = "/smoke/";
        break;
    }

//находим тайлы
    QDir dir(QString(DEF_DECORATIONS_PATH + mission + str_type));

    QStringList lst_decor(dir.entryList());
    lst_decor.removeOne(".");
    lst_decor.removeOne("..");

    if(lst_decor.count() == 0){
        qDebug() << "ERROR!! folder " << DEF_DECORATIONS_PATH + mission + str_type << "   is empty!!!";
        QApplication::closeAllWindows();
    }

    foreach(QString s, lst_decor){
        vect_pix->append(*cash->cash_pixmap->value("Res/decorations/" + mission + str_type + s));
    }

}

void DECORATIONS::set_time_line(QTimeLine *timeline)
{
    this->tl_decorations = timeline;

//настройка временной шкалы
    connect(tl_decorations, SIGNAL(valueChanged(qreal)), this, SLOT(slot_anim_forward()));

}

void DECORATIONS::set_cover(DECORATIONS *tile)
{
    cover = tile;
}

void DECORATIONS::set_basic_anim_speed(int value)
{
    basic_anim_speed = value;
}

QPainterPath DECORATIONS::shape() const
{
    return QPainterPath();
}

DECORATIONS::~DECORATIONS()
{
}

void DECORATIONS::move(MOVE_DIRECTION direction)
{
    switch (direction) {
    case LEFT:
        last_move = LEFT;
        factor_speed = basic_move_speed;
        break;

    case RIGHT:
        last_move = RIGHT;
        factor_speed = -basic_move_speed;
        break;

    default:
        break;
    }
}

void DECORATIONS::move_finish()
{
    factor_speed = 0;
}

void DECORATIONS::slot_anim_forward()
{
    if(this->pos().y() > G_H){
        this->setPos(cover->pos().x(), cover->pos().y() - this->boundingRect().height());
        return;
    }

    if(cover->pos().y() > G_H){
        cover->setPos(this->pos().x(), this->pos().y() - cover->boundingRect().height());
        return;
    }

    if(this->pos().x() + 100 > 0 && last_move == LEFT)
        factor_speed = 0;

    if((this->pos().x() + this->boundingRect().width()) - 100 < G_W && last_move == RIGHT)
        factor_speed = 0;

    this->setPos(this->pos().x() + (factor_speed*factor), this->pos().y() + basic_anim_speed);
    cover->setPos(this->pos().x() + (factor_speed*factor), cover->pos().y() + basic_anim_speed);

}


void DECORATIONS::Generate()
{
    switch (type) {
    case CLOWDS:
        Generate_Clowds();
        break;
    default:
        break;
    }
}
